package co.za.itweb.itwebpro;


import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class CitiesActivity extends Activity {
	EditText editt;
	private boolean scrolling = false;
	int data;
	public String countries[] = new String[] { "ITWeb", "Brainstorm", "IWeek",
			"CareerWeb" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.cities_layout);

		final WheelView country = (WheelView) findViewById(R.id.country);
		country.setVisibleItems(4);
		country.setViewAdapter(new CountryAdapter(this));

		country.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {
					updateCities(newValue);
				}
			}
		});

		country.addScrollingListener(new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
				updateCities(country.getCurrentItem());
			}
		});

		country.setCurrentItem(0);

		Button buttonclose = (Button) findViewById(R.id.back);
		buttonclose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				finish();
			}
		});
		 editt=(EditText) findViewById(R.id.searchterm);
		Button buttonGO = (Button) findViewById(R.id.go);
		buttonclose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				finish();

			}
		});
		
		buttonGO.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(editt.getText().toString().length()<3){
					Toast.makeText(CitiesActivity.this,"Please enter search term with minimum three characters !", Toast.LENGTH_SHORT).show();
				}else{
					
				
				Intent intent = new Intent(CitiesActivity.this, SearchActivity.class);
				intent.putExtra("data", data);
				intent.putExtra("term",editt.getText().toString());
				startActivity(intent);
				}

			}
		});
	}

	/**
	 * Updates the database query
	 */
	private void updateCities(int index) {
		data = index;
		
	}

	/**
	 * Adapter for countries
	 */
	private class CountryAdapter extends AbstractWheelTextAdapter {

		/**
		 * Constructor
		 */
		protected CountryAdapter(Context context) {
			super(context, R.layout.country_layout, NO_RESOURCE);

			setItemTextResource(R.id.country_name);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			ImageView img = (ImageView) view.findViewById(R.id.flag);
			img.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.database));
			return view;
		}

		@Override
		public int getItemsCount() {
			return countries.length;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return countries[index];
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 13) {

			if (resultCode == 1) {

			}

			if (resultCode == 0) {
				Toast.makeText(
						getApplicationContext(),
						"No search term found",
						Toast.LENGTH_SHORT).show();
				finish();

			}

		}

	}
}
